#!/bin/sh

########################################
#
# nts: NTS wrapper script to safely call NTS in Java.
#
# Copyright (C) 2001 Bernd Raichle.  Public domain.
#
#
# History:
#   2001/09/13 br v0.1
#     first version
#   2001/09/14 br v0.2
#     added -log-extension, -dvi-extension, version()
#     debugged and extended argument parsing

version=0.2

######################################################################
# abort(errmsg)
#   print `errmsg' to stderr and exit with error code 1
######################################################################
abort()
{
  echo "$progname: $1." >&2
  exit 1
}

###############################################################################
# version()
#   display version numbers and exit
###############################################################################
version()
{
  echo "$progname: NTS wrapper script $version"
  echo "--- NTS is using the following Java JDK version:"
  java -version
  echo "--- NTS and this script is using the kpathsea library:"
  kpsewhich -version
  exit 1
}

###############################################################################
# help()
#   display help message and exit
###############################################################################
help()
{
  cat <<END_OF_HELP
Usage: $progname [OPTION]... [TEXNAME[.tex]] [COMMANDS]
   or: $progname [OPTION]... \FIRST-LINE
   or: $progname [OPTION]... &FMT ARGS
END_OF_HELP

  cat <<'END_OF_HELP'
  Run NTS on TEXNAME, usually creating TEXNAME.dvi.
  Any remaining COMMANDS are processed as NTS input, after TEXNAME is read.
  If an existing .fmt file is given using &FMT or the -fmt=FMT option, use it.
  Else use `NAME.fmt', where NAME is the program invocation name, most
  commonly `tex'.  To be a bit smart, if the given .fmt does not exist and
  contains the string `nts', the format file search is tried with `nts'
  removed or replaced by the string `tex'.

  Alternatively, if the first non-option argument begins with a backslash,
  interpret all non-option arguments as a line of TeX input.

  Alternatively, if the first non-option argument begins with a &, the
  next word is taken as the FMT to read, overriding all else.  Any
  remaining arguments are processed as above.

  If no arguments or options are specified, prompt for input.

-fmt=FMTNAME             use FMTNAME instead of program name
-ini                     be NTS' initex, for dumping formats
-interaction=STRING      set interaction mode (STRING=batchmode/nonstopmode/
                          scrollmode/errorstopmode)
-kpathsea-debug=NUMBER   set path searching debugging flags according to
                          the bits of NUMBER
-log-extension=EXT       use EXT as the file name extension of the protocol
                          file (default is `log')
-dvi-extension=EXT       use EXT as the file name extension of the .dvi file
                          (default is `dvi')
-progname=STRING         set program (and fmt) name to STRING
-help                    display this help and exit
-version                 output version information and exit

Email bug reports to nts@dante.de.
END_OF_HELP

  exit
}

###############################################################################
# run_nts()
#   run NTS by calling java with NTS Jar file and appropriate arguments
###############################################################################
run_nts()
{
  #echo "run_nts(>$common_args< / >$rest_args<)"
  java $common_args -jar "$jarloc" $rest_args
}

########################################
#
# Initialize variables
#

progname=`basename $0`

prog_name=''
format_name="$progname"
interaction_mode=''
log_extension=''
dvi_extension=''

########################################
#
# Argument parsing ...
#
while
  test $# -gt 0 || break
  case "$1" in
    --progname=*|-progname=*)
        prog_name=`expr "$1" : '-*progname=\(.*\)'`
	format_name="$prog_name"
	;;
    -fmt=*|--fmt=*)
        format_name=`expr "$1" : '-*fmt=\(.*\)'` ;;
    \&*)
	format_name=`expr "$1" : '\&\(.*\)'` ;;
    --ini|-ini)
	format_name='' ;;
    --help|-help)
        help ;;
    --version|-version)
	version ;;
    --interaction=*|-interaction=*)
        interaction_mode=`expr "$1" : '-*interaction=\(.*\)'` ;;
    --kpathsea-debug=*|-kpathsea-debug=*)
	kpathsea_debug=`expr "$1" : '-*kpathsea-debug=\(.*\)'`
	export KPATHSEA_DEBUG
	KPATHSEA_DEBUG="$kpathsea_debug"
	;;
    --mktex=*|-mktex=*) ;;
    --no-mktex=*|-no-mktex=*) ;;
    --log-extension=*|-log-extension=*)
	log_extension=`expr "$1" : '-*log-extension=\(.*\)'` ;;
    --dvi-extension=*|-dvi-extension=*)
	dvi_extension=`expr "$1" : '-*dvi-extension=\(.*\)'` ;;
    "") break ;;
    *)  break ;;
  esac
do test $# -gt 0 && shift; done

########################################
#
# Test installed Java version ...
#
java_version=`java -version 2>&1 | head -1 | sed 's/^.*version "\(.*\)"/\1/'`

case "$java_version" in
  0.*|1.0*|1.1*)
    echo 'NTS needs Java JDK 1.2 or newer to work,'
    echo "Your standard Java version $java_version is too old."
    echo 'Please update to JDK 1.2 or 1.3 and make sure that the'
    echo 'command >java< points to the newer Java version.'
    exit 1
    ;;
  1.2*|1.3*)
    ;;
  "")
    echo 'Warning: Your standard Java version supplies no version number'
    echo '         I proceed with fingers crossed ...'
    ;;
  *)
    # Can we assume that NTS will run with JDK > 1.3?
    ;;
esac

########################################
#
# Locate NTS Jar file ...
#
jarfile=nts.rt.jar
jarloc=`kpsewhich --format='other binary files' --progname=nts "$jarfile"`

test -f "$jarloc" || abort "NTS Java Executable $jarfile not found"

########################################
#
# Locate given NTS format file
#
fmtloc=''

if test ! -z "$format_name"; then

  format_names="$format_name"

  # This script wants to be smart:
  # If the format name contains the string "nts",
  # add more possible format names where "nts" is removed
  # or replaced by "tex".
  # If this script is called "nts", the format "tex" can be found,
  # for "ntslatex" the format "latex" can be found etc.
  #
  case "$format_name" in
    nts*)
        new_format_name=`echo "$format_name" | sed 's/^nts//'`
        format_names="$format_names $new_format_name"
	new_format_name=`echo "$format_name" | sed 's/nts/tex/'`
        format_names="$format_names $new_format_name"
	;;
    *nts)
        new_format_name=`echo "$format_name" | sed 's/nts$//'`
        format_names="$format_names $new_format_name"
        new_format_name=`echo "$format_name" | sed 's/nts/tex/'`
        format_names="$format_names $new_format_name"
	;;
    *nts*)
        new_format_name=`echo "$format_name" | sed 's/nts/tex/'`
        format_names="$format_names $new_format_name"
	;;
  esac

  #echo "fmtnames= $format_names."

  for fmt in $format_names; do

    fmtloc=`kpsewhich --format='other binary files' --progname=nts "${fmt}.nfmt"`

    #echo "FmtLoc: $fmt => $fmtloc"

    if test ! -z "$fmtloc" -a -f "$fmtloc"; then
      #echo "found"
      format_name="$fmt"
      break;
    fi
  done

fi

#
# A format file name was given, thus we are not NTS' initex (-ini).
# If we can't find the NTS .fmt file, it is better to abort
# than leaving the user with an unexpected * prompt...
#
if test ! -z "$format_name" -a -z "$fmtloc"; then
  abort "Can't find requested NTS .fmt file >$format_name<"
fi

########################################
#
# Create Argument list for NTS
#
common_args=''
rest_args=''

#
# 1. Create special arguments 
#
test -z "$fmtloc" || \
    common_args="$common_args -Dnts.fmt=$fmtloc"
test -z "$interaction_mode" || \
    common_args="$common_args -Dnts.interaction=$interaction_mode"
test -z "$prog_name" || \
    common_args="$common_args -Dnts.progname=$prog_name"
test -z "$log_extension" || \
    common_args="$common_args -Dnts.log.extension=$log_extension"
test -z "$dvi_extension" || \
    common_args="$common_args -Dnts.dvi.extension=$dvi_extension"

#
# 2. Add all remaining arguments from command line
#
rest_args="$@"

run_nts

#
# -- end of file --
